package delete;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import db.DBManager;

public class DeleteServlet extends HttpServlet 
{
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException 
	{
		perform(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException 
	{
		perform(req, resp);
	}
	
	protected void perform(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException 
	{
		req.setCharacterEncoding("UTF-8");
		String strId = req.getParameter("id");
		int id = Integer.parseInt(strId);
		
		Connection con = DBManager.getConnection();
		
		//ユーザー削除
		try
		{
			String sql = "delete from UserAccount where id = ?";
			PreparedStatement ps = con.prepareStatement(sql);
			ps.setInt(1, id);
			ps.executeUpdate();
			ps.close();
			//一覧に戻る
			resp.sendRedirect("/Makino/UserList.jsp");
		}
		catch(SQLException e)
		{
			e.printStackTrace();
		}
	}
}
